/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */

package com.simon.mengkou.system.receiver;

import android.content.Context;

import com.google.gson.Gson;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.simon.mengkou.data.bean.base.Message;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

/**
 * 1、PushMessageReceiver是个抽象类，该类继承了BroadcastReceiver。
 * 2、需要将自定义的DemoMessageReceiver注册在AndroidManifest.xml文件中 <receiver
 * android:exported="true"
 * android:name="com.xiaomi.mipushdemo.DemoMessageReceiver"> <intent-filter>
 * <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" /> </intent-filter>
 * <intent-filter> <action android:name="com.xiaomi.mipush.ERROR" />
 * </intent-filter> </receiver>
 * 3、DemoMessageReceiver的onCommandResult方法用来接收客户端向服务器发送命令后的响应结果
 * 4、DemoMessageReceiver的onReceiveMessage方法用来接收服务器向客户端发送的消息
 * 5、onReceiveMessage和onCommandResult方法运行在非UI线程中
 * 
 *	===========================================
 *  a.不需要入库的消息都通过Notification方式推送
 *  b.需要入库的消息都通过透传方式推送
 * 
 * @author zhenshui.xia
 */
public class MiPushMessageReceiver extends PushMessageReceiver {

    @Override
    public void onCommandResult(Context context, MiPushCommandMessage message) {
    	UtilLog.d("onCommandResult is called. " + message.toString());
    }


    @Override
    public void onReceiveMessage(Context context, MiPushMessage message) {
        UtilLog.d("onReceiveMessage is called. " + message.toString());
        UtilLog.d("=========content: "+message.getContent());

        String content = message.getContent();
        if (UtilString.isNotBlank(content) && OuerApplication.mUser != null) {
//            try {
//                Gson gson = new Gson();
//            	Message msg = gson.fromJson(content, Message.class);
//                OuerApplication.mPreferences.setMessage(true);
//                OuerDispatcher.notifyMessage(context, msg);
//            } catch (Exception ex) {
//                UtilLog.e("error push msg:" + ex.getMessage());
//                ex.printStackTrace();
//            }



            OuerDispatcher.notifyMessage(context, message);
        }
    }

    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage message){
        UtilLog.d("onReceivePassThroughMessage is called. " + message.toString());

    }

    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage message){
        UtilLog.d("onNotificationMessageArrived is called." + message.toString());

    }

    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage message){
        UtilLog.d("onNotificationMessageClicked is called." + message.toString());

    }



}